package com.songtech.uitest.module.common;

import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

//可能要加上找不到元素的异常捕获
@Component
public class TabHandler {

    private Logger logger = LoggerFactory.getLogger(TabHandler.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    public void closeFirstTab(WebDriver driver) throws Exception{
//        Thread.sleep(1000);
        AutoWait.setWebDriverWait(driver);

        driver.switchTo().defaultContent();
//        WebElement i = driver.findElements(By.cssSelector("i.iconfont.icon-guanbi")).get(0);
        WebElement i = AutoWait.findAllByCss("i.iconfont.icon-guanbi").get(0);
        i.click();
//        Thread.sleep(1000);
        logger.info(space.getSpace() + "关闭了第一个标签页");
    }

    public void closeAllTabs(WebDriver driver) throws Exception{
        //这里有可能要出错，因为每删一个后，页面结构发生了改变
//        Thread.sleep(1000);
        AutoWait.setWebDriverWait(driver);

        driver.switchTo().defaultContent();
        List<WebElement> iList = driver.findElements(By.cssSelector("i.iconfont.icon-guanbi"));
//        List<WebElement> iList = AutoWait.findAllByCss("i.iconfont.icon-guanbi");
        Iterator<WebElement> it = iList.iterator();
        while (it.hasNext()){
            it.next().click();
            Thread.sleep(1000);
        }
        logger.info(space.getSpace() + "关闭了所有已打开的标签页");
    }


}
